package com.its.handle;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.its.domain.BaseResult;
import com.its.manager.pojo.Admin;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/*
* 管理员登录成功处理器
* */
public class MyAdminLoginSuccessHandle implements AuthenticationSuccessHandler {
    @Resource
    private ObjectMapper objectMapper;

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request,
                                        HttpServletResponse response,
                                        Authentication authentication) throws IOException {

        Admin admin = (Admin) authentication.getPrincipal();
        admin.setPassword(null); // 安全起见，清空密码

        BaseResult<Admin> result = new BaseResult<>(200, "登录成功", admin);

        response.setContentType("application/json;charset=utf-8");
        response.getOutputStream().write(objectMapper.writeValueAsBytes(result));
    }
}
